<?php
/*
Plugin Name: fix_upload_chinese_filename
Description: 如果上傳的檔案名稱有特殊字元，則修正為一串亂碼，以避免無法上傳的錯誤
Author: glenn
Version: 1.2
*/

function fix_filename( $fileInfo )
{

    if( $fileInfo && 
        $fileInfo['name'] && 
        !preg_match("/^[a-zA-Z0-9_\-\.]+$/u", $fileInfo['name'] ) 
    ) {

        $ext = ltrim(strrchr($fileInfo['name'], '.'), '.');

        $fileInfo['name'] = time() . '_' . uniqid();
        if( $ext ) {
            $fileInfo['name'] .= '.'.$ext;
        }

    }

    return $fileInfo;
}


add_filter('wp_handle_upload_prefilter', 'fix_filename');
// add_action();
